﻿function Room(index, onRoomUpdate, onRoomError) {
    this.index = index;
    this.root;
    this.visible = false;
    this.loader = null;
    this.total = null;
    this.adults = 2;
    this.children = 0;
    this.infants = 0;
    this.mealPlan = 1;
    this.roomType = 1;
    this.roomName = '';
    this.available = false;
    this.loading = false;
    this.allowTriple = true;
    this.allowChildren = true;
    // events
    this.OnRoomUpdated = onRoomUpdate;
    this.OnRoomError = onRoomError;
    // private vars
    this.ddlRoomType, this.ddlMealPlan, this.ddlAdults = null, this.ddlChildren = null, this.ddlInfants = null;
    this.lblChildren, this.lblInfants = null; this.divRoomError = null;
    this.imgLoader, this.h1RoomTotal = null;

    loadRoomHtml(this);

    function loadRoomHtml(room) {
        // load room template html

        var roomTemplate = $("#roomTemplate");
        roomTemplate.clone().appendTo("#divRooms").attr("id", "room" + room.index);
        room.root = $("#room" + room.index);
        room.visible = false;
        // get room controls
        room.ddlRoomType = room.root.find("#ddlRoomType");
        room.roomType = room.ddlRoomType.val();
        room.roomName = room.ddlRoomType[0].options[room.ddlRoomType[0].selectedIndex].text;
        room.divRoomError = room.root.find("#divRoomError");
        // meal plans
        room.ddlMealPlan = room.root.find("#ddlMealPlan");
        room.mealPlan = room.ddlMealPlan.val();
        //pax
        room.ddlAdults = room.root.find("#ddlAdults");
        room.ddlChildren = room.root.find("#ddlChildren");
        room.lblChildren = room.root.find("#lblChildren");
        room.ddlInfants = room.root.find("#ddlInfants");
        room.lblInfants = room.root.find("#lblInfants");
        // policy
        checkRoomPolicy(room);
        // room price
        room.imgLoader = room.root.find("#imgRoomLoader").hide();
        room.h1RoomTotal = room.root.find("#h1RoomTotal").hide();
        room.root.find("#roomTitle").html("Room #" + room.index);
        // bind events
        room.ddlRoomType.bind('change', { currentRoom: room }, function(e) {
            checkRoomPolicy(e.data.currentRoom);
            e.data.currentRoom.roomType = e.target.value;
            e.data.currentRoom.roomName = e.target.options[e.target.selectedIndex].text;
            e.data.currentRoom.OnRoomUpdated(e.data.currentRoom);
        });

        room.ddlMealPlan.bind('change', { currentRoom: room }, function(e) {
            e.data.currentRoom.mealPlan = e.target.value;
            e.data.currentRoom.OnRoomUpdated(e.data.currentRoom);
        });

        room.ddlAdults.bind('change', { currentRoom: room }, function(e) {
            if(checkRoomCapacity(e.data.currentRoom)){
                e.data.currentRoom.adults = e.target.selectedIndex;
                e.data.currentRoom.OnRoomUpdated(e.data.currentRoom);
            }
        });

        room.ddlChildren.bind('change', { currentRoom: room }, function(e) {
            if(checkRoomCapacity(e.data.currentRoom)){
                e.data.currentRoom.children = e.target.selectedIndex;
                e.data.currentRoom.OnRoomUpdated(e.data.currentRoom);
            }
        });

        room.ddlInfants.bind('change', { currentRoom: room }, function(e) {
            if(checkRoomCapacity(e.data.currentRoom)){
                e.data.currentRoom.infants = e.target.selectedIndex;
                e.data.currentRoom.OnRoomUpdated(e.data.currentRoom);
            }
        });
    }

    function checkRoomPolicy(room) {
        // check room conditions (triple occupancy and children policy)
        var optRoomType = room.ddlRoomType[0].options[room.ddlRoomType[0].selectedIndex];
        room.allowTriple = ($(optRoomType).attr("allowTriple") == "True");
        if (!room.allowTriple)
            $(room.ddlAdults[0].options[3]).hide();
        else
            $(room.ddlAdults[0].options[3]).show();
        room.allowChildren = ($(optRoomType).attr("allowChildren") == "True");
        if (!room.allowChildren || room.adults == 3) {
            room.ddlChildren.val("0").hide();
            room.children = 0;
            room.lblChildren.hide();
            room.ddlInfants.val("0").hide();
            room.infants = 0;
            room.lblInfants.hide();
        }
        else {
            room.ddlChildren.show();
            room.lblChildren.show();
            room.ddlInfants.show();
            room.lblInfants.show();
        }
    }
    
    // check maximum pax per room
    function checkRoomCapacity(room) {        
        var a = parseInt(room.ddlAdults.val());
        var c = parseInt(room.ddlChildren.val());
        var i = parseInt(room.ddlInfants.val());
        function resetRoomPax(room) {
            room.ddlAdults.val(room.adults);
            room.ddlChildren.val(room.children);
            room.ddlInfants.val(room.infants);
        };
        // single room (1 adult) + (2 children, 2 infants or 1 child and 1 infant)
        if (a == 1 &&blur (c + i) > 2) {
                alert("Too many pax for a single room!");
                resetRoomPax(room);
                return false;
        }
        //double room (2 adults) + (2 children, 2 infants or 1 child and 1 infant)
        if (a == 2 && (c + i) > 2) {
                alert("Too many pax for a double room!");
                resetRoomPax(room);
                return false;                
        }
        //triple room (3 adults only)
        if (a == 3 && (c + i) > 0) {
                alert("Too many pax for triple room! No children or infants allowed for triple rooms.");
                resetRoomPax(room);
                return false;           
        }
        // check minimum pax (1 adult or 1 child)
        if (a == 0 && c < 1) {
            alert("Room must have at lease one adult or one child!");
            resetRoomPax(room);
            return false;
        }
        // room capacity is valid
        return true;
    }

    this.setError = function(message) {
        this.available = false;
        this.divRoomError.html(message).show();
    }

    this.clearError = function(message) {
        this.available = true;
        this.divRoomError.hide();
    }
    
    this.showHtml = function() {
        alert(this.root.html());
    }

    this.toString = function() {
        var details = 'Room Idex: ' + this.index;
        details += '\nRoom type: ' + this.roomType;
        details += '\nMeal plan: ' + this.mealPlan;
        details += '\nAdults: ' + this.adults;
        details += '\nChildren: ' + this.children;
        details += '\nInfants: ' + this.infants;
        details += '\nTotal Price: ' + this.total;
        return details;
    }

    this.showRoom = function() {
        this.root.show();
        this.visible = true;
    }
    this.hideRoom = function() {
        this.root.hide();
        this.visible = false;
    }


    this.checkAvailability = function(checkIn, checkOut, numberOfRooms) {
        var loader = this.imgLoader;
        var total = this.h1RoomTotal;
        var callingRoom = this;
        total.hide();
        loader.show();
        this.loading = true;
        var userData = {
            roomTypeId: this.roomType,
            checkIn: checkIn,
            checkOut: checkOut,
            adults: this.adults,
            children: this.children,
            infants: this.infants,
            mealPlanId: this.mealPlan,
            numberOfRooms: numberOfRooms
        };

        $.ajax({
            type: "POST",
            url: '../Booking.asmx/CheckRoomAvailability',
            data: $.toJSON(userData),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function(xhr) {
                var err = eval("(" + xhr.responseText + ")");
                loader.hide();
                total.html('N/A').show();
                callingRoom.loading = false;
                callingRoom.available = false;
                callingRoom.setError('Error: ' + err.Message);
            },
            success: function(msg) {
                //debugger;
                callingRoom.loading = false;
                loader.hide();
                if (msg.d == -1) {
                    callingRoom.available = false;
                    total.html('N/A');
                    callingRoom.setError("This room is not available during selected period.");
                }
                else {
                    callingRoom.available = true;
                    callingRoom.clearError();
                    callingRoom.total = parseFloat(msg.d);
                    total.html('$' + msg.d);
                }
                total.show();
            }
        });
    }
}